﻿using STF.Blog.Dtos;
using STF.Blog.IRepository;
using STF.Blog.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace STF.Blog.Service
{
    public class SeosService : ISeosService
    {
        private ISeosManager _ise;
        public SeosService(ISeosManager ise)
        {
            _ise = ise;
        }
        public async Task<int> AddUsersAsync(string title, string keyword, string description, int webmenuId)
        {
            return await _ise.AddAsync(new Model.Seos 
            {
                Title = title,
                KeyWord = keyword,
                Description = description,
                WebMenuId = webmenuId
            });
        }

        public async Task<int> DeleteUsersAsync(int id)
        {
            var data = await _ise.GetById(id);
            if (data == null)
                return -1;
            if (data.DeleteId != 1)
                return -2;
            return await _ise.DeleteAsync(data);
        }

        public async Task<List<SeosDto>> GetAllUsersAsync(int deleteId = 1)
        {
            var data = await _ise.GetChooseAsync(sd => sd.DeleteId == deleteId);
            return data == null ? new List<SeosDto>() : data.Select(sd => new SeosDto 
            {
                Id = sd.Id,
                Title = sd.Title,
                Description = sd.Description,
                WebMenuId = sd.DeleteId,
                CreateTime = sd.CreateTime,
                UpdateTime = sd.UpdateTime
            }).ToList();
        }

        public async Task<SeosDto> GetByIdAsync(int id)
        {
            var data = await _ise.GetById(id);
            return data == null ? new SeosDto() : new SeosDto
            {
                Id = data.Id,
                Title = data.Title,
                KeyWord = data.KeyWord,
                Description = data.Description,
                WebMenuId = data.WebMenuId,
                DeleteId = data.DeleteId,
                CreateTime = data.CreateTime,
                UpdateTime = data.UpdateTime
            };
        }

        public async Task<List<SeosDto>> GetByNameAsync(string title, int deleteId = 1)
        {
            var data = await _ise.GetChooseAsync(s => s.Title.Contains(title) && s.DeleteId == deleteId);
            return data == null ? new List<SeosDto>() : data.Select(s => new SeosDto 
            {
                Id = s.Id,
                Title = s.Title,
                KeyWord = s.KeyWord,
                Description = s.Description,
                WebMenuId = s.DeleteId,
                DeleteId = s.DeleteId,
                CreateTime = s.CreateTime,
                UpdateTime = s.UpdateTime
            }).ToList();
        }

        public async Task<int> PutTrashAsync(int id)
        {
            var data = await _ise.GetById(id);
            if(data == null || data.DeleteId == 0)
                return -1;
            data.DeleteId = 0;
            return await _ise.UpdateAsync(data);
        }

        public async Task<int> RestoreAsync(int id)
        {
            var data = await _ise.GetById(id);
            if (data == null || data.DeleteId == 1)
                return -1;
            data.DeleteId = 1;
            return await _ise.UpdateAsync(data);
        }

        public async Task<int> UpdateUsersAsync(int id, string title, string keyword, string description, int webmenuId)
        {
            var data = await _ise.GetById(id);
            if (data == null)
                return -1;
            data.Title = title;
            data.Description = description;
            data.WebMenuId = webmenuId;
            data.UpdateTime = DateTime.Now;
            return await _ise.UpdateAsync(data);
        }
    }
}
